package com.qcby.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.framwork.config.SystemConstants;
import com.qcby.framwork.utils.BeanCopyUtils;
import com.qcby.framwork.utils.ResponseResult;
import com.qcby.front.dao.SgCategoryMapper;
import com.qcby.front.form.AddArticleForm;
import com.qcby.front.form.ArticleForm;
import com.qcby.front.model.SgArticle;
import com.qcby.front.dao.SgArticleMapper;
import com.qcby.front.model.SgArticleTag;
import com.qcby.front.model.SgCategory;
import com.qcby.front.service.SgArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.front.service.SgCategoryService;
import com.qcby.front.utils.RedisCache;
import com.qcby.front.vo.*;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-01
 */
@Service
public class SgArticleServiceImpl extends ServiceImpl<SgArticleMapper, SgArticle> implements SgArticleService {
    @Autowired
    private SgCategoryService sgCategoryService;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SgArticleService sgArticleService;
    @Override
    public ResponseResult hotArticleList() {
        //查询热门文章 封装成ResponseResult返回，封装查询条件
        LambdaQueryWrapper<SgArticle> queryWrapper = new LambdaQueryWrapper<>();
        //必须是正式文章0是正式文章 1是草稿  后面是进行的对于0的优化
        queryWrapper.eq(SgArticle::getStatus,
                SystemConstants.ARTICLE_STATUS_NORMAL);
        //按照浏览量进行排序 降序排序
        queryWrapper.orderByDesc(SgArticle::getViewCount);
        //最多只查询10条
        Page<SgArticle> page = new Page(1,10);
        page(page,queryWrapper);
        //获取到查询的列表数据
        List<SgArticle> articles = page.getRecords();
        //bean拷贝
        List<HotArticleVo> articleVos = new ArrayList<>();
        for (SgArticle article : articles) {
            HotArticleVo vo = new HotArticleVo();
            BeanUtils.copyProperties(article,vo);
            articleVos.add(vo);
        }
        //直接用静态方法查询到具体的数据
        return ResponseResult.okResult(articleVos);
    }

    @Override
    public ResponseResult articleList(Integer pageNum,Integer pageSize,Long categoryId){
        LambdaQueryWrapper<SgArticle> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //判空。如果前端传了categoryId这个参数，那么查询时要和传入的相同。第二个参数是数据表的文章id，第三个字段是前端传来的文章id
        lambdaQueryWrapper.eq(Objects.nonNull(categoryId)&&categoryId>0,SgArticle::getCategoryId,categoryId);
        //只查询状态是正式发布的文章。Article实体类的status字段跟0作比较，一样就表示是正式发布的
        lambdaQueryWrapper.eq(SgArticle::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //对istop字段进行降序排序，实现置顶的文章istop=1在最前面
        lambdaQueryWrapper.orderByDesc(SgArticle::getIsTop);

        //分页查询
        Page<SgArticle> page=new Page<>(pageNum,pageSize);
        page(page,lambdaQueryWrapper);

        //查询categoryname
        List<SgArticle> articles=page.getRecords();
        for(SgArticle sgArticle:articles){
            SgCategory sgCategory=sgCategoryService.getById(sgArticle.getCategoryId());
            sgArticle.setCategoryName(sgCategory.getName());
        }
        //最后查询的结果封装在vo里面
        List<ArticleListVo> articleListVos= BeanCopyUtils.copyBeanList(page.getRecords(), ArticleListVo.class);

        //查询分类名称要获取分类的id

        //封装在pagevo
        PageVo pageVo=new PageVo(articleListVos,page.getTotal());
        return ResponseResult.okResult(pageVo);

    }

    @Override
    public ResponseResult getArticleDetail(Long id){
        //根据id查询文章
        SgArticle sgArticle=getById(id);
        //从redis中获取viewCount
//        Integer viewCount=redisCache.getCacheMapValue("sgArticle:viewCount",id.toString());
//        sgArticle.setViewCount(viewCount.longValue());
        //转换成VO
        ArticleDetailVo articleDetailVo=BeanCopyUtils.copyBean(sgArticle, ArticleDetailVo.class);
        //根据分类id查询分类名
        Long catehoryId=articleDetailVo.getCategoryId();
        SgCategory sgCategory=sgCategoryService.getById(catehoryId);
        if(sgArticle!=null){
            articleDetailVo.setCategoryName(sgCategory.getName());
        }
        //封装响应返回
        return ResponseResult.okResult(articleDetailVo);
    }
    @Override
    public ResponseResult updateViewCount(Long id) {
        //更新redis中对应 id的浏览量
        redisCache.incrementCacheMapValue("article:viewCount",id.toString(),1);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult add(AddArticleForm addArticleForm) {
        //添加博客
        SgArticle sgArticle=BeanCopyUtils.copyBean(addArticleForm,SgArticle.class);
        save(sgArticle);
        List<SgArticleTag> articleTags=addArticleForm.getTags().stream()
                .map(tagId->new SgArticleTag(sgArticle.getId(),tagId))
                .collect(Collectors.toList());

        //添加博客和标签的关联
        return null;
    }

    @Override
    public PageVo selectArticlePage(SgArticle article, Integer pageNum, Integer pageSize) {
        return null;
    }

    @Override
    public ArticleVo getInfo(Long id) {
        return null;
    }

    @Override
    public void edit(ArticleForm articleForm) {

    }

}


